package com.geek.novel.adapter

import android.app.Activity
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.geek.novel.R
import com.geek.novel.activity.base.BaseActivity
import com.geek.novel.activity.book.BookDetailActivity
import com.geek.novel.entity.BookRead
import com.geek.novel.ui.BookImageView
import com.geek.novel.utils.BookUtil.formatAuthorName
import com.geek.novel.utils.BookUtil.formatBookName
import com.geek.novel.utils.BookUtil.removeBookDescBlank
import com.geek.novel.utils.FormatUtil

/**查询书籍列表的适配器*/
class BookSearchListAdapter(context: Activity, list: ArrayList<BookRead>)
    : BaseRecyclerViewAdapter<BookSearchListAdapter.ViewHolder, BookRead>(context, list) {

    //定义内部类ViewHolder，并继承RecyclerView.ViewHolder传入View参数通常是Recycler View子项的最外层布局
    class ViewHolder(view: View, adapter: BaseRecyclerViewAdapter<BaseRecyclerViewHolder, Any>): BaseRecyclerViewHolder(view, adapter) {
        val imageView: BookImageView
        val tvBookName: TextView
        val tvBookDesc: TextView
        val tvBookOtherInfo: TextView
        init {
            imageView = view.findViewById(R.id.iv_today_book_image)
            tvBookName = view.findViewById(R.id.tv_today_book_name)
            tvBookDesc = view.findViewById(R.id.tv_today_book_desc)
            tvBookOtherInfo = view.findViewById(R.id.tv_today_book_other_info)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.listview_book_today, parent, false)
        return ViewHolder(view, this as BaseRecyclerViewAdapter<BaseRecyclerViewHolder, Any>)
    }


    /**绑定容器显示控件内容*/
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        //设置书籍名称、书籍最新章节、书籍图片等等数据
        val book = list[position]

        val bookName = book.bookName
        val bookDesc = book.bookDesc

        val imgId = book.imgId
        holder.imageView.setBookName(bookName)
        holder.imageView.loadBookImage(imgId)

        //书名
        holder.tvBookName.text = formatBookName(bookName)

        //书籍描述(替换掉空格)
        holder.tvBookDesc.text = removeBookDescBlank(bookDesc)

        val authorName = formatAuthorName(book.authorName)

        //其他信息：作者 · 类型 · 连载 · 13万字
        val otherInfo = StringBuilder()
        otherInfo.append(authorName)
        if (!TextUtils.isEmpty(book.categoryStr)) {
            otherInfo.append(" · ")
            otherInfo.append(book.categoryStr)
        }

        otherInfo.append(" · ")
        otherInfo.append(if (book.bookStatus == 0) "连载" else "完结")

        //字数
        val wordCount = book.wordCount
        if (wordCount > 0) {
            otherInfo.append(" · ")
            val wordCountStr = FormatUtil.formatNumberCount(wordCount, 1)
            otherInfo.append(wordCountStr).append("字")
        }
        holder.tvBookOtherInfo.text = otherInfo.toString()

        //调用父类的方法，主要用于绑定事件
        super.onBindViewHolder(holder, position)

    }

    /**列表项的点击事件*/
    override fun itemClickEvent(data: BookRead) {
        //前往书籍详情页面
        this.gotoBookDetailPage(data)
    }

    /**
     * 前往书籍详情页面
     */
    private fun gotoBookDetailPage(book: BookRead){
        val params = Bundle()
        params.putString("bookId", book.bookId)
        context as BaseActivity
        context.startActivityByAnim(context, BookDetailActivity::class.java, params)
    }

}